package com.cb.myzhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cb.myzhxy.mapper.StudentMapper;
import com.cb.myzhxy.pojo.Admin;
import com.cb.myzhxy.pojo.LoginForm;
import com.cb.myzhxy.pojo.Student;
import com.cb.myzhxy.service.StudentService;
import com.cb.myzhxy.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
*
*/
@Service("studentServiceImpl")
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Override
    public Student login(LoginForm loginForm) {
        //创建QueryWrapper对象
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        //拼接查询条件
        queryWrapper.eq(Student::getName,loginForm.getUsername());
        // 转换成密文进行查询
        queryWrapper.eq(Student::getPassword, MD5.encrypt(loginForm.getPassword()));
        Student student = baseMapper.selectOne(queryWrapper);
        return student;

    }

    @Override
    public Student getStudentById(Long userId) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getId,userId);
        Student student = baseMapper.selectOne(queryWrapper);
        return student;
    }

    @Override
    public IPage<Student> getStudentByOpr(Page<Student> page, Student student) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        if(student!=null){
            if(student.getClazzName()!=null){
                queryWrapper.eq(Student::getClazzName,student.getClazzName());
            }
            if(student.getName()!=null){
                queryWrapper.like(Student::getName,student.getName());
            }
            queryWrapper.orderByDesc(Student::getId);
            queryWrapper.orderByAsc(Student::getName);
        }
        //创建分页对象
        Page<Student> page1 = baseMapper.selectPage(page, queryWrapper);
        return page1;
    }
}
